﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Game.Foundation.ChineseChess
{
    public class Chessboard
    {
        private Shuai _shuai;
        private Shuai _jiang;

        protected List<ChessmanBase> _chessmanList;

        public static readonly int ColumnCount = 9;

        public static readonly int RowCount = 10;

        public event EventHandler Completed;

        public Chessboard()
        {
            this._chessmanList = new List<ChessmanBase>();
            ChessmanBase item;

            item = new Shuai(this, Camp.Red, "帅");
            item.SetLocation(5, 1);
            this._shuai = item as Shuai;
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Shuai(this, Camp.Black, "将");
            item.SetLocation(5, 10);
            this._jiang = item as Shuai;
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Shi(this, Camp.Red, "士");
            item.SetLocation(4, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Shi(this, Camp.Red, "士");
            item.SetLocation(6, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Xiang(this, Camp.Red, "相", 1);
            item.SetLocation(3, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Xiang(this, Camp.Red, "相", 1);
            item.SetLocation(7, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ma(this, Camp.Red, "马");
            item.SetLocation(2, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ma(this, Camp.Red, "马");
            item.SetLocation(8, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ju(this, Camp.Red, "车");
            item.SetLocation(1, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ju(this, Camp.Red, "车");
            item.SetLocation(9, 1);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Pao(this, Camp.Red, "炮");
            item.SetLocation(2, 3);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Pao(this, Camp.Red, "炮");
            item.SetLocation(8, 3);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Red, "兵", 1, 4);
            item.SetLocation(1, 4);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Red, "兵", 1, 4);
            item.SetLocation(3, 4);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Red, "兵", 1, 4);
            item.SetLocation(5, 4);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Red, "兵", 1, 4);
            item.SetLocation(7, 4);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Red, "兵", 1, 4);
            item.SetLocation(9, 4);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ju(this, Camp.Black, "车");
            item.SetLocation(1, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ma(this, Camp.Black, "马");
            item.SetLocation(2, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Xiang(this, Camp.Black, "象", 10);
            item.SetLocation(3, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Shi(this, Camp.Black, "士");
            item.SetLocation(4, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Shi(this, Camp.Black, "士");
            item.SetLocation(6, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Xiang(this, Camp.Black, "象", 10);
            item.SetLocation(7, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ma(this, Camp.Black, "马");
            item.SetLocation(8, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Ju(this, Camp.Black, "车");
            item.SetLocation(9, 10);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Pao(this, Camp.Black, "炮");
            item.SetLocation(2, 8);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Pao(this, Camp.Black, "炮");
            item.SetLocation(8, 8);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Black, "卒", -1, 7);
            item.SetLocation(1, 7);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Black, "卒", -1, 7);
            item.SetLocation(3, 7);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Black, "卒", -1, 7);
            item.SetLocation(5, 7);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Black, "卒", -1, 7);
            item.SetLocation(7, 7);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);

            item = new Bing(this, Camp.Black, "卒", -1, 7);
            item.SetLocation(9, 7);
            this._chessmanList.Add(item);
            //container.Controls.Add(item);
        }

        public ChessmanBase GetChessman(int x, int y)
        {
            ChessmanBase item = null;
            foreach (ChessmanBase chess in this._chessmanList)
            {
                if (chess.XLocation == x && chess.YLocation == y)
                {
                    item = chess;
                    break;
                }
            }

            return item;
        }

        void item_Disposed(object sender, EventArgs e)
        {
            Shuai shuai = sender as Shuai;
            this.OnComplete();
        }

        public virtual bool MoveChessman(ChessmanBase chess, int Xdest, int Ydest)
        {
            if (!this._chessmanList.Contains(chess)) return false;

            if (chess.CanMoveTo(Xdest, Ydest) && !this.IsLeaderAgainst(chess.XLocation, chess.YLocation, Xdest, Ydest))
            {
                ChessmanBase dest = this.GetChessman(Xdest, Ydest);
                if (dest != null && dest.Camp != chess.Camp)
                {
                    this._chessmanList.Remove(dest);
                    if (dest is Shuai)
                    {
                        this.OnComplete();
                    }
                }

                chess.SetLocation(Xdest, Ydest);
                return true;
            }

            return false;
        }

        public bool MoveChessman(int XOld, int YOld, int Xdest, int Ydest)
        {
            return this.MoveChessman(GetChessman(XOld, YOld), Xdest, Ydest);
        }

        private bool IsLeaderAgainst(int xOld, int yOld, int xNew, int yNew)
        {
            int x1 = this._jiang.XLocation, y1 = this._jiang.YLocation, x2 = this._shuai.XLocation, y2 = this._shuai.YLocation;
            bool result = x1 == x2;
            if (result)
            {
                result = x1 != xNew;
                if (result)
                {
                    for (int y = Math.Min(y1, y2) + 1; y < Math.Max(y1, y2); y++)
                    {
                        if (this.GetChessman(x1, y) != null && (x1 != xOld || y != yOld))
                        {
                            return false;
                        }

                    }
                }
            }

            return result;
        }

        protected void OnComplete()
        {
            if (this.Completed != null)
            {
                Completed(this, EventArgs.Empty);
            }
        }
    }
}
